#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, time, re, urllib2

import pygadu, Packet
import funkcje

from PyQt4 import QtCore, QtGui

class GaduGadu(pygadu.SimpleTextSession):
    def __init__(self, uin, password, textbox):
        pygadu.SimpleTextSession.__init__(self, uin, password)

        self.funkcje = funkcje.funkcje
        
        self.textbox = textbox

    def onMessageReceived(self, uin, message, seq):
        self.textbox.append(QtCore.QString("Dostalem wiadomosc od "+str(uin)+
                                           " o tresci \""+message+"\""))
        polecenie = False
        for nazwa, funkcja in self.funkcje.iteritems():
            if funkcja.re.match(message):
                self.textbox.append(QtCore.QString("Uznalem, ze to funkcja "+nazwa))
                print "regulka: "+repr(funkcja.re.findall(message))
                return_data = funkcja.wykonaj(funkcja.re.findall(message))
                self.textbox.append(QtCore.QString("Wysylam odpowiedz klientowi."))
                self.sendMessage(uin, return_data, clazz = 0x0008)
                polecenie = True

        if polecenie == False:
            self.sendMessage(uin, u"Nie znam takiej komendy, może spróbuj 'help'?", clazz = 0x0008)

    def onLoginOk(self):
        self.sendBuddyList(self.buddy_list.list)
        self.changeStatus(Packet.NewStatus.AVAIL_D, u"M'kayyy!")

class GGThread(QtCore.QThread):
    def __init__(self, uin, password, textbox):
        QtCore.QThread.__init__(self)
        self.textbox = textbox
        self.uin = uin
        self.password = password
        self.stop = False
        
    def run(self):
        self.gadu = GaduGadu(self.uin, self.password, self.textbox)
        self.gadu.connect()
        self.gadu.main_loop()
        return
    
class ConnectDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)

        self.setWindowTitle("Polacz...")
        self.gridlayout = QtGui.QGridLayout()
        label1 = QtGui.QLabel(self.tr("Numer GG"))
        label2 = QtGui.QLabel(self.tr("Haslo"))
        self.numer = QtGui.QLineEdit()
        self.numer.setValidator(QtGui.QIntValidator(1000, 9999999, self))
        self.haslo = QtGui.QLineEdit()
        self.accept = QtGui.QPushButton(self.tr("OK"))
        self.reject = QtGui.QPushButton(self.tr("Anuluj"))

        self.connect(self.accept, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("accept()"))
        self.connect(self.reject, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("reject()"))        
        
        self.gridlayout.addWidget(label1, 0, 0)
        self.gridlayout.addWidget(label2, 1, 0)
        self.gridlayout.addWidget(self.numer, 0, 1, 1, 2)
        self.gridlayout.addWidget(self.haslo, 1, 1, 1, 2)
        self.gridlayout.addWidget(self.accept, 2, 1)
        self.gridlayout.addWidget(self.reject, 2, 2)        

        self.setLayout(self.gridlayout)

class StatusDialog(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)

        self.setWindowTitle("Ustaw status...")
        self.gridlayout = QtGui.QGridLayout()
        label1 = QtGui.QLabel(self.tr("Opis"))
        self.status = QtGui.QLineEdit()
        self.accept = QtGui.QPushButton(self.tr("OK"))
        self.reject = QtGui.QPushButton(self.tr("Anuluj"))

        self.connect(self.accept, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("accept()"))
        self.connect(self.reject, QtCore.SIGNAL("clicked()"), self, QtCore.SLOT("reject()"))        
        
        self.gridlayout.addWidget(label1, 0, 0)
        self.gridlayout.addWidget(self.status, 0, 1, 1, 2)
        self.gridlayout.addWidget(self.accept, 2, 1)
        self.gridlayout.addWidget(self.reject, 2, 2)        

        self.setLayout(self.gridlayout)
        
class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        
        w = QtGui.QWidget()
        self.setCentralWidget(w)

        self.textbox = QtGui.QTextEdit()
        self.textbox.setReadOnly(True)
        vbox = QtGui.QVBoxLayout()
        vbox.setMargin(5)
        vbox.addWidget(self.textbox)
        w.setLayout(vbox)

        self.createActions()
        self.createMenus()
        
        self.setWindowTitle(self.tr("Bot GG"))
        self.setMinimumSize(160,160)
        self.resize(480,320)

    def conn(self):
        dialog = ConnectDialog()
        if dialog.exec_() == 1:
            numer = int(dialog.numer.text())
            haslo = str(dialog.haslo.text())
            self.textbox.append(self.tr("Bot GG by Adam Michalik \n"
                                        "Lacze z serwerem GG... uin: "+str(numer)+
                                        " haslo: "+haslo))
            
            self.ggthread = GGThread(numer, haslo, self.textbox)
            self.ggthread.start()

    def stat(self):
        dialog = StatusDialog()
        if dialog.exec_() == 1:
            status = unicode(dialog.status.text())
            self.textbox.append(self.tr("Zmieniam status na %s" % status.encode('utf-8')))
            
            self.ggthread.gadu.changeStatus(Packet.NewStatus.AVAIL_D, status)
            
    def createActions(self):
        self.connAct = QtGui.QAction(self.tr("&Polacz..."), self)
        self.connAct.setStatusTip(self.tr("Polacz z serwerem GG"))
        self.connect(self.connAct, QtCore.SIGNAL("triggered()"), self.conn)

        self.statAct = QtGui.QAction(self.tr("&Zmien status..."), self)
        self.statAct.setStatusTip(self.tr("Ustaw status bota"))
        self.connect(self.statAct, QtCore.SIGNAL("triggered()"), self.stat)

        self.exitAct = QtGui.QAction(self.tr("&Zamknij"), self)
        self.exitAct.setStatusTip(self.tr("Zamknij program"))
        self.connect(self.exitAct, QtCore.SIGNAL("triggered()"), self.close)
      
    def createMenus(self):
        self.fileMenu = self.menuBar().addMenu(self.tr("&Akcje"))
        self.fileMenu.addAction(self.connAct)
        self.fileMenu.addAction(self.statAct)
        self.fileMenu.addSeparator()
        self.fileMenu.addAction(self.exitAct)
 

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    mainwindow = MainWindow()
    mainwindow.show()
    sys.exit(app.exec_())
